package cn.zlj.pethome.order.controller;

import cn.zlj.pethome.basic.QueryResult;
import cn.zlj.pethome.basic.domain.Logininfo;
import cn.zlj.pethome.basic.dto.PayDto;
import cn.zlj.pethome.basic.util.AjaxResult;
import cn.zlj.pethome.basic.util.LoginContext;
import cn.zlj.pethome.order.service.IOrderProductService;
import cn.zlj.pethome.order.domain.OrderProduct;
import cn.zlj.pethome.order.query.OrderProductQuery;

import cn.zlj.pethome.org.domain.TEmployee;
import cn.zlj.pethome.org.mapper.EmployeeMapper;
import cn.zlj.pethome.product.domain.Product;
import cn.zlj.pethome.user.domain.User;
import cn.zlj.pethome.user.mapper.UserMapper;
import cn.zlj.pethome.user.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/orderProduct")
public class OrderProductController {
    @Autowired
    public IOrderProductService orderProductService;
    @Autowired
    public UserMapper userService;
    @Autowired
    private EmployeeMapper employeeMapper;

    @PostMapping("/cancel/{id}")
    public AjaxResult cancel(@PathVariable("id") Long id){
        //添加订单
       orderProductService.cancel(id);
        return AjaxResult.me().success();
    }
    //confirm
    @PostMapping("/confirm")
    public AjaxResult confirm(@RequestBody PayDto payDto,HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        TEmployee employee = employeeMapper.selectByLogininfoId(logininfo.getId());
        payDto.setEmployee(employee);
        orderProductService.confirm(payDto);
        return AjaxResult.me().success();
    }

    @PostMapping("/shopping")
    public AjaxResult shopping(@RequestBody Product product, HttpServletRequest request){
      //添加订单
        Logininfo logininfo = LoginContext.getLogininfo(request);
        User user = userService.findByLogininfo(logininfo.getId());
        product.setUserId(user.getId());
        orderProductService.shopping(product);
            return AjaxResult.me().success();
    }

    /**
     * 保存和修改公用的
     * @param orderProduct  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody OrderProduct orderProduct){
        try {
            if( orderProduct.getId()!=null)
                orderProductService.update(orderProduct);
            else
                orderProductService.add(orderProduct);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            orderProductService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public OrderProduct get(@PathVariable("id")Long id)
    {
        return orderProductService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<OrderProduct> list(){

        return orderProductService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public QueryResult<OrderProduct> json(@RequestBody OrderProductQuery query)
    {
        return orderProductService.queryByPage(query);
    }
}
